<?php
require_once ('../includes/SmartyManager.class.php');
require_once ('../includes/DB.class.php');
require_once ('../includes/exceptions' . DS . 'AppExceptions.class.php');
require_once ('../includes/LogInserter.class.php');

$smarty = SmartyManager::getSmarty ();
$dels = array ();
foreach ( $_POST as $key => $val ) {
	if (stristr ( $key, "del" )) {
		array_push ( $dels, "id = $val" );
	}
}

if (! empty ( $dels )) {
	try {
		$delStats = join ( ' or ', $dels );
		DB::beginTransaction ();
		$consignmentFileQuery = "select filename, id from little_consignment_file where $delStats";
		DB::query ( $consignmentFileQuery );
		$consignmentFileResult = DB::getResult ();
		while ( $consignmentFileRow = $consignmentFileResult->fetch_assoc () ) {
			LogInserter::logForDelete ( "删除这个发货批次文件：({$consignmentFileRow['filename']})" );
			$consignmentQuery = "select certificationNumber, orderNumber, orderSubitemNumber, purchaseNumber from little_consignment where fileId = '{$consignmentFileRow['id']}'";
			DB::query($consignmentQuery);
			$consignmentResult = DB::getResult();
			while( $consignmentRow = $consignmentResult->fetch_assoc()){
				$mainSql = "update little_main set consignmentBatch = NULL 
				where ";
				$wheres = array();
				if($consignmentRow['certificationNumber']){
					array_push($wheres, "certificationNumber = '{$consignmentRow['certificationNumber']}'");
				}
				if($consignmentRow['orderNumber']){
					array_push($wheres, "orderNumber = '{$consignmentRow['orderNumber']}'");
				}
				if($consignmentRow['orderSubitemNumber']){
					array_push($wheres, "orderSubitemNumber = '{$consignmentRow['orderSubitemNumber']}'");
				}
				if($consignmentRow['purchaseNumber']){
					array_push($wheres, "purchaseNumber = '{$consignmentRow['purchaseNumber']}'");
				}
				$where = join(' and ',$wheres);
				$mainSql .= $where;
				DB::query($mainSql);
				
				if($consignmentRow['certificationNumber']){
					$rukuSql = "update little_ruku set consignmentBatch = NULL
						where certificationNumber = '{$consignmentRow['certificationNumber']}'";
					DB::query($rukuSql);
					$chukuSql = "update little_chuku set consignmentBatch = NULL 
						where certificationNumber = '{$consignmentRow['certificationNumber']}'";
					
					DB::query($chukuSql);					
				}
			}
			$deleteConsignmentSql = "delete from little_consignment where fileId = {$consignmentFileRow['id']}";
			DB::query($deleteConsignmentSql);
		}
			$deleteConsignmentFileSql = "delete from little_consignment_file where $delStats";
			DB::query ( $deleteConsignmentFileSql );
			DB::commit ();
	} catch ( Exception $e ) {
		DB::rollback ();
		$smarty->assign ( "errTitle", '删除发货批次文件失败' );
		$smarty->assign ( 'errorType', 'consignmentDelete' );
		$smarty->assign ( 'errMsg', $e );
		$smarty->display ( 'error.html' );
		die ();
	}
}

$smarty->assign ( "successTitle", '删除发货批次文件成功' );
$smarty->assign ( 'successType', 'consignmentDelete' );
$smarty->assign ( 'successMsg', '删除文件成功' );
$smarty->display ( 'success.html' );

?>